import cherrypy
import os
import json
import time
import random

class Root(object) : pass

class Dummy(object) : pass

class TestSuccess(object):
    exposed=True

    def __init__(self, content = {}) :
        self.content = content

    def GET(self, *argv, **kargv) :
        time.sleep(1)
        return '["success"]'

class TestFailure(object):
    exposed=True

    def __init__(self, content = {}) :
        self.content = content

    def GET(self, *argv, **kargv) :
        time.sleep(1)
        raise cherrypy.HTTPError(status="500", message="Error in the server side")

class Recognize(object) :
    exposed=True

    def __init__(self) :
        pass

    def POST(self, *argv, **kargv) :
        print  "data received:"
        print argv, kargv
        body = cherrypy.request.body.read()
        print cherrypy.request.headers
        print 'body is', body
        return '100'
        

if __name__ == '__main__':

    current_dir = os.path.dirname(os.path.abspath(__file__))
    static_dir = os.path.join(current_dir, r'recorder')
    print static_dir
    cherrypy.config.update({'environment': 'production',
                            'server.socket_host' : '0.0.0.0',
                            'server.socket_port' : 8888,
                            'server.thread_pool' : 10,
                            'log.error_file': 'site.log',
                            'log.screen': True,
                            })

    conf = {
            '/':{
                'tools.staticdir.on': True,
                'tools.staticdir.dir': static_dir,
                'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
                }
    }

    root = Dummy()
    root.test = Dummy()
    root.test.success = TestSuccess()
    root.test.failure = TestFailure()
    root.dsr = Dummy()
    root.dsr.rec = Recognize()

    cherrypy.tree.mount(root, '/', config=conf)
    cherrypy.engine.start()
    cherrypy.engine.block()

